package com.controller;

import java.io.File;



import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.Scope;
import com.server.ScopeServer;
import com.server.RecordServer;
import com.server.SysuserServer;
import com.util.PageBean;

@Controller
public class ScopeController {
	@Resource
	private ScopeServer scopeService;
	@Resource
	private SysuserServer userService;
	@Resource
	private RecordServer recordService;


	@RequestMapping("admin/addScope.do")
	public String addScope(HttpServletRequest request, Scope Scope, HttpSession session) {
			scopeService.add(Scope);
			return "redirect:scopeList.do";
	}
// 处理更新分班级
	@RequestMapping("admin/doUpdateScope.do")
	public String doUpdateScope(ModelMap map, int id) {
		map.put("jz", scopeService.getById(id));
		return "admin/scope_update";
	}
	
	//查看班级信息
	@RequestMapping("admin/scope_detail.do")
	public String scope_detail(ModelMap map, int id) {
		map.put("jz", scopeService.getById(id));
		return "admin/scope_detail";
	}
// 保存分班级信息
	@RequestMapping("admin/updateScope.do")
	public String updateScope(HttpServletRequest request, Scope scope) {
		scopeService.update(scope);
		return "redirect:scopeList.do";
	}

	// 分页查询
	@RequestMapping("admin/scopeList.do")
	public String jianList(@RequestParam(value = "page", required = false) String page, ModelMap map,
			HttpSession session) {

		if (page == null || page.equals("")) {
			page = "1";
		}
		PageBean pageBean = new PageBean(Integer.parseInt(page), PageBean.PAGESIZE);
		Map<String, Object> pmap = new HashMap<String, Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", pageBean.getPageSize());
		Map<String, Object> cmap = new HashMap<String, Object>();
		int total = scopeService.getCount(cmap);
		pageBean.setTotal(total);
		List<Scope> list = scopeService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		map.put("ulist", userService.getAll(null));
		Map<String, Object> umap = new HashMap<>();
		umap.put("utype", "培训师");
		umap.put("status", "空闲中");
		map.put("slist", userService.getAll(umap));
		return "admin/scope_list";
	}

	// 分页模糊查询
	@RequestMapping("admin/vagueScopeList.do")
	public String vagueScopeList(@RequestParam(value = "page", required = false) String page, ModelMap map,
			HttpSession session, Scope cd) {
		if (page == null || page.equals("")) {
			page = "1";
		}
		PageBean pageBean = new PageBean(Integer.parseInt(page), PageBean.PAGESIZE);
		Map<String, Object> pmap = new HashMap<String, Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", pageBean.getPageSize());
		Map<String, Object> cmap = new HashMap<String, Object>();

		int total = scopeService.getCount(cmap);
		pageBean.setTotal(total);
		List<Scope> list = scopeService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 2);
		Map<String, Object> umap = new HashMap<>();
		umap.put("utype", "培训师");
		umap.put("status", "空闲中");
		map.put("slist", userService.getAll(umap));
		return "admin/scope_list";
	}

	@RequestMapping("admin/deleteScope.do")
	public String deleteScope(int id,HttpSession session) {
			scopeService.delete(id);
			return "redirect:scopeList.do";
	}
	

	// 文件上传
	public String fileUpload(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request, String img) {
		String path = request.getSession().getServletContext().getRealPath("upload");
		String fileName = file.getOriginalFilename();
		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		try {
			file.transferTo(targetFile);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String pa = request.getContextPath() + "/upload/" + fileName;
		System.out.println("path===" + pa);
		if (fileName != null && !fileName.equals("")) {
			img = fileName;
		} else {
			img = "zanwu.jpg";
		}
		return img;

	}
}
